package com.ft.oa.web.sys;

import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.domain.dto.sys.MenuDto;
import com.ft.oa.domain.sys.SysAuth;
import com.ft.oa.service.sys.SysAuthService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单接口
 */
@RestController
@RequestMapping("/auth")
public class SysAuthController {

    @Resource
    private SysAuthService sysAuthService;


    /**
     * 用户菜单树
     * @return
     */
    @GetMapping("/menu")
    public ApiResult list(){

        List<MenuDto> result = sysAuthService.menuTree();

        return ApiResult.successWithData(result);
    }

    /**
     * 用户菜单树
     * @return
     */
    @GetMapping("/menuNew")
    public ApiResult listNew(){

        List<MenuDto> result = sysAuthService.menuTreeNew();

        return ApiResult.successWithData(result);
    }

    /**
     * 用户权限树
     * @return
     */
    @GetMapping("/tree")
    public ApiResult tree(){

        List<SysAuth> result = sysAuthService.tree();

        return ApiResult.successWithData(result);
    }

    /**
     * 一级菜单列表
     * @return
     */
    @GetMapping("/menu/top")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult top(){

        List<SysAuth> result = sysAuthService.top();

        return ApiResult.successWithData(result);
    }

    /**
     * 编辑菜单
     * @return
     */
    @PostMapping("/edit")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult edit(@RequestBody SysAuth sysAuth){

        sysAuthService.edit(sysAuth);

        return ApiResult.simpleSuccess();
    }

    /**
     * 删除菜单
     * @return
     */
    @PostMapping("/del/{id}")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult del(@PathVariable Integer id){

        sysAuthService.del(id);

        return ApiResult.simpleSuccess();
    }

    /**
     * 菜单上移
     * @return
     */
    @PostMapping("/menu/up/{id}")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult menuUp(@PathVariable Integer id){

        sysAuthService.menuUp(id);

        return ApiResult.simpleSuccess();
    }

    /**
     * 菜单下移
     * @return
     */
    @PostMapping("/menu/down/{id}")
    @PreAuthorize("@ss.havePermission()")
    public ApiResult menuDown(@PathVariable Integer id){

        sysAuthService.menuDown(id);

        return ApiResult.simpleSuccess();
    }



}
